package com.scs.application.modules.dept.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.annotation.MatrCanPass;
import com.scs.application.modules.base.dto.ExpandColumnDTO;
import com.scs.application.modules.dept.dto.OprtApplyPackDTO;
import com.scs.application.modules.dept.entity.OprtApplyItem;
import com.scs.application.modules.dept.service.OprtApplyItemService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 */
@RestController
@RequestMapping("/dept/oprt_apply_item")
public class OprtApplyItemController extends CrudController<OprtApplyItemService, OprtApplyItem>{

    @PostMapping("addByTemp")
    public RestResult addByTemp(@RequestBody OprtApplyPackDTO oprtApplyPackDTO) {
        return RestResult.ok("添加成功", service.addByTemp(oprtApplyPackDTO));
    }

    @Override
    @GetMapping("/page")
    public PageResult page() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Page<OprtApplyItem> page = (Page<OprtApplyItem>) service.page(queryContext);
        this.fillDataPage(page);
        return PageResult.ok("查询成功", page.getRecords() , page.getTotal());
    }

    /**
     * 添加/更新数据
     */
    @PostMapping("/save")
    @MatrCanPass(businessType = "warehousing_item")//手动入库耗材禁用校验
    public RestResult saveItem(@Valid @RequestBody OprtApplyItem entity) {

        boolean isOk = service.saveOrUpdate(entity);
        this.fillDataOne(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        return RestResult.ok("数据保存成功",  entity);
    }

    private void fillDataOne (OprtApplyItem itemPack ) {
        itemPack.setChildData(service.list(Wrappers.<OprtApplyItem>query().eq("pack_apply_item_id",itemPack.getId()).isNotNull("pack_apply_item_id")));
    }

    private void fillDataPage (Page<OprtApplyItem> page ) {
        if (page.getRecords() == null || page.getRecords().size() == 0) {
            return;
        }
        List<OprtApplyItem> itemList = page.getRecords();

        // 包
        List<OprtApplyItem> itemPackList = itemList.stream().filter(item -> StringUtils.isNotBlank(item.getPackId())).collect(Collectors.toList());
        if (ObjectUtil.length(itemPackList) == 0) {
            return;
        }

        List<String> packItemIdLIst = itemPackList.stream().map(OprtApplyItem::getId).collect(Collectors.toList());

        // 包明细
        Map<String,List<OprtApplyItem>> itemChildList = service.list(Wrappers.<OprtApplyItem>query().isNotNull("pack_apply_item_id").in("pack_apply_item_id", packItemIdLIst))
                .stream()
                .collect(Collectors.groupingBy(OprtApplyItem::getPackApplyItemId));

        List<ExpandColumnDTO> childCols;
        ExpandColumnDTO expandColumnDTO;

        for(OprtApplyItem packItem : itemPackList){
            packItem.setShowExpand(true);
            packItem.setChildData(itemChildList.get(packItem.getId()));

            childCols = new ArrayList<>();
            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("matrName").setTitle("商品名称").setColumnWidth(220);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("matrSpec").setTitle("商品规格").setColumnWidth(100);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("packageUnit").setTitle("包装单位").setColumnWidth(80);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("packageQty").setTitle("包装数量").setColumnWidth(80);
            childCols.add(expandColumnDTO);

            expandColumnDTO = new ExpandColumnDTO();
            expandColumnDTO.setField("amount").setTitle("金额").setColumnWidth(80);
            childCols.add(expandColumnDTO);
            packItem.setChildCols(childCols);
        }
    }
}
